package ge.telasi.tasks.ui.user;

import ge.telasi.tasks.model.Group;
import ge.telasi.tasks.model.validation.GroupValidator;
import ge.telasi.tasks.ui.log.Loggable;
import ge.telasi.tasks.ui.log.Logger;
import ge.telasi.tasks.ui.log.LoggerUtils;
import javax.swing.JComponent;
import javax.swing.JPanel;

/**
 * @author dimitri
 */
public class GroupPanel extends JPanel implements Loggable {

    /** Creates new form GroupPanel */
    public GroupPanel() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtName = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtDescription = new javax.swing.JTextArea();

        jLabel1.setText("დასახელება:");

        jLabel2.setText("აღწერილობა:");

        txtName.setName("Group.name"); // NOI18N

        txtDescription.setColumns(20);
        txtDescription.setLineWrap(true);
        txtDescription.setRows(5);
        txtDescription.setWrapStyleWord(true);
        txtDescription.setName("Group.description"); // NOI18N
        jScrollPane1.setViewportView(txtDescription);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(0, 0, 0)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 298, Short.MAX_VALUE)
                    .addComponent(txtName, javax.swing.GroupLayout.DEFAULT_SIZE, 298, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea txtDescription;
    private javax.swing.JTextField txtName;
    // End of variables declaration//GEN-END:variables

    private Logger logger;
    private Group group;

    public Logger getLogger() {
        return logger;
    }

    public void setLogger(Logger logger) {
        this.logger = logger;
    }

    public JComponent[] allFields() {
        return new JComponent[] { txtName, txtDescription };
    }

    public Group getGroup() {
        Group formGroup = new Group();
        if (group != null) {
            formGroup.setId(group.getId());
        }
        formGroup.setName(txtName.getText());
        formGroup.setDescription(txtDescription.getText());
        return formGroup;
    }

    public void setGroup(Group group) {
        this.group = group;
        reset();
    }

    private void reset() {
        if (group == null) {
            txtName.setText("");
            txtDescription.setText("");
        } else {
            txtName.setText(group.getName());
            txtDescription.setText(group.getDescription());
        }
    }

    public boolean validateForm() {
        LoggerUtils.clearLogger(logger, allFields());
        Group formGroup = getGroup();
        try {
            new GroupValidator().validateFull(formGroup);
            return true;
        } catch (Exception ex) {
            LoggerUtils.manageException(this, logger, ex, allFields());
            return false;
        }
    }

}
